import axios from "axios";
import { message,navigate } from '@/components/useApp'

const service = axios.create({
  baseURL: "/",
  timeout: 100000,
});

// 请求拦截
service.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem("Authorization");
    config.headers["Authorization"] = token;
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  response => {
    const res = response.data;
    if(res.code === 401){
      message.error('登录过期，即将跳转登录');
      setTimeout(()=>{
        navigate('/')
      },3000)
    }
    return res;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
